package com.star.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.exception.ServiceException;
import com.star.exception.SystemException;
import com.star.pojo.Comment;
import com.star.pojo.dto.BlogListReq;
import com.star.pojo.vo.DetailedBlog;
import com.star.pojo.vo.FirstPageBlog;
import com.star.pojo.vo.RecommendBlog;
import com.star.result.*;
import com.star.service.BlogService;
import com.star.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页控制器
 *
 * @author tanyapeng
 * @date 2021/2/5
 */
@Controller
@Api(tags = "首页博客接口")
@Slf4j
public class IndexController {

    @Resource
    private BlogService blogService;

//    @Resource`
//    private TypeService typeService;

    @Resource
    private CommentService commentService;

    @ApiOperation(value = "所有博客接口", notes = "博客列表")
    @GetMapping("/allBlogList")
    @ResponseBody
    public PagingApiRes getBlogList(BlogListReq req) {
        try {
            List<FirstPageBlog> allFirstPageBlog = blogService.getAllFirstPageBlog(req);
            if (CollectionUtils.isEmpty(allFirstPageBlog)) {
                throw new ServiceException(20001, "博客列表不能为空");
            }
            int count = blogService.getAllFirstPageBlogCount();
            PagingApiRes res = PagingApi.ok(allFirstPageBlog, count);
            res.setTotalPage(Paging.totalPage(req, res));
            res.setPage(req.getPage());
            return res;
        } catch (Exception e) {
            log.error("getBlogList error - {}", ExceptionUtils.getStackTrace(e));
            throw new SystemException(10001, "获取博客列表出错");
        }
    }

    @ApiOperation(value = "推荐博客接口", notes = "推荐博客列表")
    @GetMapping("/recommendedBlogList")
    @ResponseBody
    public ApiRes getRecommendedBlogList() {
        try {
            List<RecommendBlog> recommendedBlog = blogService.getRecommendedBlog();
            if (CollectionUtils.isEmpty(recommendedBlog)) {
                throw new ServiceException(20001, "推荐博客列表不能为空");
            }
            return API.ok(recommendedBlog);
        } catch (Exception e) {
            log.error("getRecommendedBlogList error - {}", ExceptionUtils.getStackTrace(e));
            throw new SystemException(10001, "获取推荐博客列表出错");
        }
    }

//    /**
//     * 分页查询博客列表
//     *
//     * @param model
//     * @param pageNum
//     * @param attributes
//     * @return
//     */
//    @GetMapping("/")
//    public String index(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, RedirectAttributes attributes) {
//        PageHelper.startPage(pageNum, 10);
//        List<FirstPageBlog> allFirstPageBlog = blogService.getAllFirstPageBlog();
//        List<RecommendBlog> recommendedBlog = blogService.getRecommendedBlog();
//
//        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(allFirstPageBlog);
//        model.addAttribute("pageInfo", pageInfo);
//        model.addAttribute("recommendedBlogs", recommendedBlog);
//
//        return "index";
//    }

    /**
     * 搜索博客
     *
     * @param model
     * @param pageNum
     * @param query
     * @return
     */
    @PostMapping("/search")
    public String search(Model model,
                         @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                         @RequestParam String query) {
        PageHelper.startPage(pageNum, 1000);
        List<FirstPageBlog> searchBlog = blogService.getSearchBlog(query);
        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(searchBlog);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("query", query);
        return "search";
    }

    /**
     * 跳转博客详情页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        DetailedBlog detailedBlog = blogService.getDetailedBlog(id);
        List<Comment> comments = commentService.listCommentByBlogId(id);
        model.addAttribute("comments", comments);
        model.addAttribute("blog", detailedBlog);
        return "blog";
    }

//    /**
//     * 最新博客列表
//     *
//     * @param model
//     * @return
//     */
//    @GetMapping("/footer/newblog")
//    public String newblogs(Model model) {
//        List<FirstPageBlog> newBlog = blogService.getNewBlog();
//        model.addAttribute("newblogs", newBlog);
//        return "index :: newblogList";
//    }

    /**
     * 博客信息
     *
     * @param model
     * @return
     */
    @GetMapping("/footer/blogmessage")
    public String blogMessage(Model model) {
        Integer blogTotal = blogService.getBlogTotal();
        Integer blogViewTotal = blogService.getBlogViewTotal();
        Integer blogCommentTotal = blogService.getBlogCommentTotal();
        Integer blogMessageTotal = blogService.getBlogMessageTotal();

        model.addAttribute("blogTotal", blogTotal);
        model.addAttribute("blogViewTotal", blogViewTotal);
        model.addAttribute("blogCommentTotal", blogCommentTotal);
        model.addAttribute("blogMessageTotal", blogMessageTotal);
        return "index :: blogMessage";
    }
}